
var ResizableColumns = new Class({
  initialize: function(grip,col,header) {
    this.column = col;
    this.grip = grip;

    var w = header.getStyle('width');
    this.width = /(\d+)\s*px/.exec(w)[1].toInt(); // what to do if not match?
    this.bound = {
      drag: this.drag.bind(this),
      stop: this.stop.bind(this)
    };
    grip.addEvent('mousedown', this.start.bind(this));
  },

  start: function(e) {
    // FIXME what if the viewport scroll...
    // if it don't scroll, do the scroll ourself
    this.ori_x = e.client.x;
//    this.ori_w = this.column.getStyle('width'); // must be set first
    document.addEvents({mousemove: this.bound.drag, mouseup: this.bound.stop});
    return false;
  },
  drag: function(e) {
    this.column.setStyle('width',this.width+e.client.x-this.ori_x);
    return false;
  },
  stop: function(e) {
    document.removeEvent('mousemove',  this.bound.drag);
    document.removeEvent('mouseup',  this.bound.stop);
    this.width = this.width+e.client.x-this.ori_x;
    this.column.setStyle('width',this.width);
    return false;
  }
});

function OnPageLoad() {

  /* not commited
  var gw = new GridView('');
  var dm = new CollectionModel;
  gw.injectSectionContent(dm.getData('',''),$('gw'));
  */


  var resizable_header = $$('div.gridview th div.gripwrapper');
  resizable_header.each(function(header,index) {
/*    var wrapper = new Element('div', {'class': 'gripwrapper'});
    wrapper.wraps(header);
    grip.inject(header);*/
    var grip = new Element('div', {'class': 'hgrip'});
    grip.inject(header,'bottom');
    //if(!header.hasClass('.col'+(index+1))) alert('error');
    var griphandler = new ResizableColumns(grip,$$('.col'+(index+1)),header);
  });

  $$('div.section').addEvent('click', function(e) {
    $(e.target).toggleClass('closed');
    return false;
  });

  new Request.JSON({
      url:"../sql_request.php",
//      method: 'post',
      onSuccess: function(data) {   console.log(JSON.encode(data)); },
      onFailure: function() { console.log('rq failed'); }
    }).post({'request':'select * from biblio_author'});
}

window.addEvent('domready', OnPageLoad);

